// EOCommand.cs
//
//  Copyright (C) 2008 Whitemice Consulting
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Whitemice.EOMono
{
  public abstract class EOCommand
  {
    protected string report;
    
    public EOCommand() 
    {
      report = string.Empty;
    } // end ctor
    
    [EOArguementAttribute("report", "")]
    public string Report
    {
      get { return report; }
    } // end Report
    
    internal void ReportLine(string format, params object[] args)
    {
      report = string.Format("{0}\n{1}", report, string.Format(format, args));
    } // end ReportLine
    
    internal void ReportText(string format, params object[] args)
    {
      report = string.Format("{0}{1}", report, string.Format(format, args));
    } // end ReportText
    
    protected virtual void DoArguements(object[] args)
    {
      IDictionary<string, PropertyInfo> properties;
      int count = 0;
      string k;
      object v;
      
      if (args != null)
      {
        properties = new Dictionary<string, PropertyInfo>();
        foreach(PropertyInfo property in this.GetType().GetProperties())
        {
          object[] eoArgs;
          
          eoArgs = property.GetCustomAttributes(typeof(EOArguementAttribute), true);
          foreach(EOArguementAttribute arg in (eoArgs as EOArguementAttribute[]))
          {
            properties.Add(arg.Parameter.ToLower(), property);
          } // end foreach-EOArguementAttribute
        } // end foreach-property
        while (count < args.Length)
        {
          k = args[count].ToString();
          count++;
          v = args[count];
          count++;
          if (properties.ContainsKey(k.ToLower()))
            properties[k.ToLower()].SetValue(this, v, null);
        } // end while
        k = null;
        v = null;
        args = null;
        properties = null;
      } // end if-args-not-null
    } // end DoArguements
    
    protected abstract void Execute();
  } // end class
}